<?php 
class CategoryController extends AuthController{
	public $db;
	public $category;
	//栏目ID
	public $cid;

	public function __init(){
		$this->db=K('Category');
		//读取缓存，存放在属性category
		$this->category=S('category');
		$this->cid=Q('cid',0,'intval');

		if($this->cid && !isset($this->category[$this->cid])){
			$this->error('栏目不存在');
		}		
	}

	//显示栏目列表
	public function index(){
		//框架提供的方法，数组进行整数映射转换
		int_to_string($this->category,array('is_show',array(0=>'隐藏',1=>'显示')));
		$this->assign('category',$this->category);
		$this->display();
	}

	//栏目添加
	public function add(){
		if(IS_POST){
			if($this->db->addCategory()){
				$this->success('栏目添加成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			//添加是要选择栏目，将栏目数据分配过去
			$this->assign('category',$this->category);
			$gt=S('goods_type');
			$this->assign('goods_type',$gt);
			$this->display();
		}
	}
	//栏目编辑
	public function edit(){
		if(IS_POST){
			if($this->db->editCategory()){
				$this->success('栏目编辑成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			//分配栏目表用于修改顶级栏目
			// $this->assign('category',$this->category);
			//显示需要进行编辑的一条数据
			$field=$this->db->find($this->cid);
			/*
			编辑的栏目数据处理
			1)父栏目不能是子栏目及其当前栏目
			2)父栏目添加selected属性
			*/
			$category=$this->category;
			foreach ($category as $key => $c) {
				$category[$key]['selected']=$c['cid']==$field['pid']?"selected=''":'';
				$category[$key]['disabled']=Data::isChild($category,$c['cid'],$field['cid'])||$c['cid']==$field['cid']?"disabled=''":'';
			}
			$this->assign('category',$category);
			$this->assign('field',$field);
			$this->display();
		}
	}
	
	//栏目删除
	public function del(){
		if($this->db->delCategory()){
			$this->success('删除成功','index');
		}else{
			$this->error($this->db->error);
		}
	}

	//更新缓存
	public function upCache(){
		if($this->db->updateCache()){
			$this->success('缓存更新成功','index');
		}else{
			$this->error($this->db->error);
		}
	}


}

?>